package com.yehuishou.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.Address;
import com.yehuishou.pojo.Order;
import com.yehuishou.pojo.QueryVO;
import com.yehuishou.pojo.Zulinorder;
import com.yehuishou.service.SaleOrderService;
import com.yehuishou.service.ZLService;

@Controller
public class ZLinController {

	@Autowired
	private SaleOrderService saleOrderService;
	@Autowired
	private ZLService zlService;
	@Autowired
	private SaleOrderService sos;

	@RequestMapping("zldd")
	public String SaleOrder(@RequestParam(required=true,defaultValue="1") Integer pageNum,
			@RequestParam(required=true,defaultValue="10") Integer pageSize,QueryVO vo,HttpServletRequest request,ModelMap mm) {
		vo.getOrder().setOrder_type(3);
		PageInfo<QueryVO> mv= saleOrderService.findUserOrder(pageNum,pageSize,vo);
		if(vo != null && vo.getOrder() != null){
			SimpleDateFormat onetime =new SimpleDateFormat("yyyy-MM-dd");
			if(vo.getOrder().getStartdate() != null) {
				Date startDate = vo.getOrder().getStartdate();
				String format2 = onetime.format(startDate.getTime());
				request.setAttribute("onetime", format2);
			}
			if(vo.getOrder().getEnddate() != null) {
				Date startDate1 = vo.getOrder().getEnddate();
				String format = onetime.format(startDate1.getTime());
				request.setAttribute("endtime", format);
			}
		}
		//将信息传到页面
		request.setAttribute("mv", mv);
		//数据回显
		request.setAttribute("vo", vo);
		return "zlin";
	}
	
	//跳修改详情
	@RequestMapping("zloedit")
	public String zloedit(String oid , Model model) {
		Zulinorder zlo = zlService.findByZO(oid);
		model.addAttribute("zlo", zlo);
		return "zloedit";
	}
	
	//修改详情
	@ResponseBody
	@RequestMapping("zloeditadd")
	public Boolean zloeditadd(Zulinorder zlo) {
		zlService.odeit(zlo);
		return true;
	}
	
	//跳修改地址
	@RequestMapping("zldedit")
	public String zldedit(Integer uid , Model model) {
		Address addr = zlService.findByDU(uid);
		model.addAttribute("addr", addr);
		return "zldedit";
	}
	
	//修改地址
	@ResponseBody
	@RequestMapping("zldeditadd")
	public Boolean zldeditadd(Address addr) {
		zlService.ddeit(addr);
		return true;
	}
	
	//跳修改快递
	@RequestMapping("zlkedit")
	public String zlkedit(String oid , Model model) {
		Order ord = zlService.findByO(oid);
		model.addAttribute("ord", ord);
		return "zlkedit";
	}
	
	//修改快递
	@ResponseBody
	@RequestMapping("zlkeditadd")
	public Boolean zlkeditadd(Order ord) {
		zlService.kdeit(ord);
		return true;
	}
	
	//删除订单
	@ResponseBody
	@RequestMapping("delzlo")
	public Boolean delzlo(String oid) {
		zlService.delo(oid);
		zlService.delzlo(oid);
		return true;
	}
	
	//跳修改快递
	@RequestMapping("payedit")
	public String payedit(String oid , Model model) {
		Order ord = zlService.findByO(oid);
		model.addAttribute("ord", ord);
		return "zpayedit";
	}
	
	@RequestMapping("zldddq")
	public String zldq(@RequestParam(required=true,defaultValue="1") Integer pageNum,
			@RequestParam(required=true,defaultValue="10") Integer pageSize , Integer ghm , Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<Zulinorder> list = zlService.findByDQ(ghm);
		PageInfo<Zulinorder> p = new PageInfo<Zulinorder>(list);
		model.addAttribute("page", p);
		model.addAttribute("ghm", ghm);
		return"zlinddd";
	}
	
	@ResponseBody
	@RequestMapping("chag")
	public boolean chang(Integer ghm , String oid) {
		zlService.updateg(ghm,oid);
		return true;
	}
	
	//批量删除订单
	// 批量删除的ajax
	@ResponseBody
	@RequestMapping("/deleteZLin")
	public boolean deleteZLin(@RequestParam(value = "td_s[]") String [] td_s) {
		zlService.deleteZl(td_s);
		sos.deleteUsers(td_s);
		return true;
			
	}  
}
